package cn.chenhuanming.chuanzhi.controller;

import cn.chenhuanming.chuanzhi.service.ShareService;
import cn.chenhuanming.chuanzhi.service.StaffService;
import cn.chenhuanming.chuanzhi.service.VoteService;
import cn.chenhuanming.chuanzhi.util.authority.AuthorityTool;
import cn.chenhuanming.chuanzhi.util.entity.Share;
import cn.chenhuanming.chuanzhi.util.entity.Vote;
import cn.chenhuanming.chuanzhi.util.file.ImageUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017-05-06.
 */
@Controller
public class DispatcherController {

    @Autowired
    ShareService shareService;

    @Autowired
    StaffService staffService;

    @Autowired
    VoteService voteService;


    @GetMapping(value = {"/signIn.html"})
    String signIn(){
        if(AuthorityTool.isAuthenticated())
            return "redirect:/index.html?size=5&sort=publishTime,desc";
        return "signIn";
    }

    @GetMapping(value = {"/index.html","/"})
    String index(Pageable pageable, Model model){
        model.addAttribute("share",shareService.listShare(pageable));
        model.addAttribute("page",pageable.getPageNumber());
        model.addAttribute("page_share",true);
        return "index";
    }

    @GetMapping(value = {"/search"})
    String search(String keyword,Pageable pageable, Model model){
        model.addAttribute("share",shareService.search(keyword,pageable));
        model.addAttribute("page",pageable.getPageNumber());
        model.addAttribute("page_share",true);
        return "index";
    }

    @GetMapping("/staff.html")
    String staff( Model model){
        model.addAttribute("page_staff",true);
        return "staff";
    }

    @GetMapping("/me.html")
    String me(Model model){
        model.addAttribute("me",staffService.findOne(AuthorityTool.getPrincipal()));
        model.addAttribute("page_me",true);
        return "me";
    }

    @GetMapping("/newShare.html")
    String editorShare(Model model){
        model.addAttribute("page_newShare",true);
        return "newShare";
    }

    @GetMapping("/password.html")
    String changePassword(Model model){
        model.addAttribute("page_password",true);
        return "password";
    }

    @GetMapping("/share/{id}")
    String share(@PathVariable String id,Model model){
        Share share = shareService.findShare(id);
        if(share==null)
            return "404";
        model.addAttribute("share",share);
        if(AuthorityTool.isAuthenticated())
            model.addAttribute("canDelete",share.getStaff().getUsername().equals(AuthorityTool.getPrincipal()));
        return "share";
    }

    @GetMapping("/vote/{id}")
    String vote(@PathVariable String id,Model model){
        Vote vote = voteService.findOne(id);
        if(vote==null)
            return "404";
        model.addAttribute("vote",vote);
        model.addAttribute("canVote",voteService.isCanVote(vote));
        model.addAttribute("canFinish",vote.getStatus()==1&&vote.getStaff().getUsername().equals(AuthorityTool.getPrincipal()));
        return "vote";
    }

    @GetMapping("newVote.html")
    String newVote(Model model){
        model.addAttribute("page_newVote",true);
        return "newVote";
    }

    @GetMapping("/voteList.html")
    String voteList(Pageable pageable,Model model){
        model.addAttribute("page_voteList",true);
        model.addAttribute("vote",voteService.listVote(pageable));
        model.addAttribute("page",pageable.getPageNumber());
        return "voteList";
    }

    @PostMapping("/newVote")
    String newVote(String title, String content, String options){
        List<String> list = Arrays.asList(options.split(",|，"));
        if(title.equals("")||list.size()<=1)
            return "redirect:/newVote.html";
        voteService.newVote(title, content, list);
        return "redirect:/voteList.html?size=5&sort=createDate,desc";
    }

    /**
     * ckeditor图片上传
     *
     * @Title imageUpload
     * @param request
     * @param response
     */
    @RequestMapping("/imageUpload")
    public void imageUpload(HttpServletRequest request, HttpServletResponse response) {
        String DirectoryName = "static/upload/";
        try {
            ImageUploadUtil.ckeditor(request, response, DirectoryName);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
